﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_MainList : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("Pro_MainChange.aspx", "添加新项目");

                BindGrid(GetWhere());
            }
        }


        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<Proj_Main>().Where(wc).Count();

            List<Proj_Main> items = TbsDbSession.Default.From<Proj_Main>().Where(wc).OrderBy(Proj_Main._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();
            //if (!string.IsNullOrEmpty(Request["t"]) && items.Count == 0)
            //{
            //    //PageContext.RegisterStartupScript("top.addUTab('/Project/Pro_MainList.aspx?t=1', 'noproj_x1', '未创建合同的项目', '');");

            //}

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            //if (!string.IsNullOrEmpty(Request["did"]))
            //{
            //    wc = wc && Adm_BASICD._.BATYPE == int.Parse(Request["did"]);
            //}
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (Proj_Main._.PRJNUB.Contain(ttbxMyBox2.Text) || Proj_Main._.DESTON.Contain(ttbxMyBox2.Text) || Proj_Main._.CUTNAM.Contain(ttbxMyBox2.Text));
            }
            if (!string.IsNullOrEmpty(txt_StartTime.Text))
            {
                wc = wc && Proj_Main._.AddTime > txt_StartTime.Text;
            }
            if (!string.IsNullOrEmpty(txt_EndTime.Text))
            {
                wc = wc && Proj_Main._.AddTime < txt_EndTime.Text;
            }


            if (drp_Status.SelectedValue != "-1")
                wc = wc && Proj_Main._.STATUS == int.Parse(drp_Status.SelectedValue);
            if (!string.IsNullOrEmpty(Request["t"]))
            {

                List<VI_C1> listc = TbsDbSession.Default.From<VI_C1>().Where(VI_C1._.C == 0).ToList();
                int[] listint = (from p in listc select p.ID).ToArray();
                if (listint.Length > 0)
                    wc = wc && Proj_Main._.ID.SelectIn<int>(listint);
                else
                {
                    PageContext.RegisterStartupScript("top.__doPostBack('hid_btn', '');top.RemoveT('noproj_x');");
                }
            }


            if (!string.IsNullOrEmpty(Request["s"]))
            {

                List<VI_C6> listc = TbsDbSession.Default.From<VI_C6>().Where(VI_C6._.C == 1).ToList();
                int[] listint = (from p in listc select p.ID).ToArray();
                if (listint.Length > 0)
                    wc = wc && Proj_Main._.ID.SelectIn<int>(listint);
                else
                {
                    PageContext.RegisterStartupScript("top.__doPostBack('hid_btn', '');top.RemoveT('M_nowproj');");
                }
            }


            return wc;
        }
        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid(GetWhere());
        }
        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string stuts = "";
            string st = e.Values[8].ToString();
            switch (st)
            {
                case "0":
                    stuts = "项目建立";
                    break;
                case "1":
                    stuts = "合同建立";
                    break;
                case "2":
                    stuts = "项目完成";
                    break;
                case "10":
                    stuts = "已作废";
                    break;
            }
            e.Values[8] = stuts;
            //string color = e.Values[7].ToString();
            //string bold = e.Values[8].ToString();
            //if (bold == "True")
            //    bold = "bold";
            //else
            //    bold = "";
            //string title = "<span style=\"color:{0};font-weight:{1}\">" + e.Values[0] + "</span>";
            //title = string.Format(title, color, bold);
            //e.Values[0] = title;
            //e.Values[7] = "<a href=\"javascript:;\" onclick=\"TabView('/AfterSale/ASale_RePairChange.aspx?id=" + e.Values[7] + "', 'tab_wxadd', '修改维修记录', '/icon/comment_edit.png');\"><img src=\"/icon/comment_edit.png\" /></a>";
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                Proj_Main pm = new Proj_Main();
                pm.ID = id;
                pm.Attach();
                pm.STATUS = "10";
                TbsDbSession.Default.Update<Proj_Main>(pm);
                //TbsDbSession.Default.Delete<Proj_Main>(Proj_Main._.ID == id);
                Alert.ShowInTop("项目已作废！");
                BindGrid(GetWhere());
            }
        }

        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            string n = Grid1.Rows[e.RowIndex].Values[7].ToString();
            if (n == "已作废")
            {
                Alert.Show("该项目已作废！");
            }
            else
            {
                if (n == "项目完成")
                {
                    Alert.Show("该项目已完工！");
                }
                else
                {
                    int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
                    PageContext.RegisterStartupScript(Window1.GetShowReference("Pro_MainChange.aspx?id=" + id, "修改"));
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int[] list = Grid1.SelectedRowIndexArray;
            if (list.Length > 0)
            {
                int index = list[0];
                string s = Grid1.Rows[index].Values[7].ToString();
                if (s != "项目完成")
                {
                    FormInfo.GridEdit(Grid1, Window1, "Pro_MainChange.aspx");
                    // BindGrid(GetWhere());
                }
                else
                {
                    if (s == "已作废")
                    {
                        Alert.Show("该项目已作废！");
                    }
                    else
                        Alert.Show("该项目已完工！");
                }
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            int[] list = Grid1.SelectedRowIndexArray;
            if (list.Length > 0)
            {
                int del = 0;
                foreach (int item in list)
                {
                    int index = item;
                    int id = int.Parse(Grid1.DataKeys[index][0].ToString());
                    Proj_Main pm = new Proj_Main();
                    pm.ID = id;
                    pm.Attach();
                    pm.STATUS = "10";
                    del = TbsDbSession.Default.Update<Proj_Main>(pm);
                }
                if (del > 0)
                    Alert.Show("项目已作废！");
            }
            else
                Alert.Show("请选择一项进行作废处理！");

            BindGrid(GetWhere());
        }

        protected void btnContatAdd_Click(object sender, EventArgs e)
        {
            string ok = Window2.GetShowReference("Pro_ContatChange.aspx", "添加项目合同"); ;
            int[] list = Grid1.SelectedRowIndexArray;
            if (list.Length > 0)
            {
                int index = list[0];
                string id = Grid1.DataKeys[index][0].ToString();
                string st = Grid1.DataKeys[index][1].ToString();
                ok = Window2.GetShowReference("Pro_ContatChange.aspx?cid=" + id, "添加项目合同");
                Proj_Contat pc = TbsDbSession.Default.From<Proj_Contat>().Where(Proj_Contat._.PRJNUB == st).ToFirst();
                if (pc != null)
                {
                    PageContext.RegisterStartupScript(Confirm.GetShowReference("该项目下已经存在一个合同，是否需要继续增加？", "提示", MessageBoxIcon.Question, ok, ""));
                }
                else
                    PageContext.RegisterStartupScript(ok);
            }
            else
                PageContext.RegisterStartupScript(ok);
        }


        protected void btnView_Click(object sender, EventArgs e)
        {
            FormInfo.GridView(Grid1, "/Project/Pro_ContatList.aspx", "pid", "/icon/icon-form-search.gif", "查看相关合同");
        }

        protected void btnSampleAdd_Click(object sender, EventArgs e)
        {
            WindowAttribute wa = new WindowAttribute();
            wa.Title = "申请出样费用";
            FormInfo.GridEdit(Grid1, Window1, "Pro_SampleChange.aspx", wa, "pid");
        }

        protected void btnSampleView_Click(object sender, EventArgs e)
        {
            FormInfo.GridView(Grid1, "/Project/Pro_SampleList.aspx", "pid", "/icon/icon-form-search.gif", "查看相关出样费用");
        }

        protected void Grid1_RowSelect(object sender, GridRowSelectEventArgs e)
        {

            string n = Grid1.Rows[e.RowIndex].Values[5].ToString();
            if (n == "已作废")
            {
                btnContat.Enabled = false;
                btnSample.Enabled = false;
                Button1.Enabled = false;
                Button2.Enabled = false;
            }
            else
            {
                btnContat.Enabled = true;
                btnSample.Enabled = true;
                Button1.Enabled = true;
                Button2.Enabled = true;
            }
        }

        protected void btn_TaskAdd_Click(object sender, EventArgs e)
        {
            WindowAttribute wa = new WindowAttribute();
            wa.Title = "添加后续任务";
            FormInfo.GridEdit(Grid1, Window1, "Pro_TaskAdd.aspx", wa, "pid");
            //PageContext.RegisterStartupScript(Window1.GetShowReference("Pro_TaskAdd.aspx"));
        }

        protected void btn_TaskView_Click(object sender, EventArgs e)
        {
            FormInfo.GridView(Grid1, "/Project/Pro_TaskList.aspx", "pid", "/icon/icon-form-search.gif", "查看后续任务");
        }

        protected void Search_Change(object sender, EventArgs e)
        {
            BindGrid(GetWhere());
        }

        protected void MenuButton1_Click(object sender, EventArgs e)
        {
            WindowAttribute wa = new WindowAttribute();
            wa.Title = "添加项目完工档案";
            FormInfo.GridEdit(Grid1, Window1, "Pro_CompletChange.aspx", wa, "pid");
        }

        protected void MenuButton2_Click(object sender, EventArgs e)
        {
            FormInfo.GridView(Grid1, "/Project/Pro_CompletList.aspx", "pid", "/icon/icon-form-search.gif", "查看项目完工档案");
        }
    }
}